import {
  IsString,
  IsNotEmpty,
  IsOptional,
  IsEmail,
  IsEnum,
  IsNumber,
  IsBoolean,
  IsArray,
  IsDateString,
  MinLength,
  MaxLength,
  Min,
  Max,
  Matches
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type, Transform } from 'class-transformer';

// 登录 DTO
export class LoginDto {
  @ApiProperty({
    description: '用户名或邮箱',
    example: 'admin'
  })
  @IsString()
  @IsNotEmpty()
  usernameOrEmail: string;

  @ApiProperty({
    description: '密码',
    example: 'admin123'
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  password: string;
}

// 用户信息 DTO
export class UserInfoDto {
  @ApiProperty({ description: '用户ID' })
  id: number;

  @ApiProperty({ description: '用户名' })
  username: string;

  @ApiPropertyOptional({ description: '昵称' })
  nickName?: string;

  @ApiProperty({ description: '邮箱' })
  email: string;

  @ApiPropertyOptional({ description: '电话' })
  phone?: string;

  @ApiPropertyOptional({ description: '头像' })
  avatar?: string;

  @ApiProperty({ description: '主要角色' })
  role: string;

  @ApiProperty({ description: '权限列表', type: [String] })
  permissions: string[];

  @ApiProperty({ description: '角色列表', type: [String] })
  roles: string[];
}

// 创建用户 DTO
export class CreateUserDto {
  @ApiProperty({
    description: '用户名',
    example: 'johndoe',
    minLength: 3,
    maxLength: 50
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(3)
  @MaxLength(50)
  @Matches(/^[a-zA-Z0-9_-]+$/, {
    message: '用户名只能包含字母、数字、下划线和连字符'
  })
  username: string;

  @ApiPropertyOptional({
    description: '昵称',
    example: 'John Doe',
    maxLength: 100
  })
  @IsOptional()
  @IsString()
  @MaxLength(100)
  nickName?: string;

  @ApiProperty({
    description: '邮箱',
    example: 'john@example.com'
  })
  @IsEmail()
  @IsNotEmpty()
  email: string;

  @ApiProperty({
    description: '密码',
    example: 'password123',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  password: string;

  @ApiPropertyOptional({
    description: '手机号',
    example: '13800138000'
  })
  @IsOptional()
  @IsString()
  @Matches(/^1[3-9]\d{9}$/, {
    message: '请输入有效的手机号码'
  })
  phone?: string;

  @ApiPropertyOptional({
    description: '头像URL',
    example: 'https://example.com/avatar.jpg'
  })
  @IsOptional()
  @IsString()
  avatar?: string;

  @ApiPropertyOptional({
    description: '性别',
    example: 0,
    enum: [0, 1]
  })
  @IsOptional()
  @IsEnum([0, 1])
  gender?: number;

  @ApiPropertyOptional({
    description: '状态',
    example: 1,
    enum: [0, 1]
  })
  @IsOptional()
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '个性签名',
    maxLength: 200
  })
  @IsOptional()
  @IsString()
  @MaxLength(200)
  signature?: string;

  @ApiPropertyOptional({
    description: '地址',
    maxLength: 500
  })
  @IsOptional()
  @IsString()
  @MaxLength(500)
  address?: string;

  @ApiPropertyOptional({
    description: '生日',
    example: '1990-01-01'
  })
  @IsOptional()
  @IsDateString()
  birthDate?: string;

  @ApiPropertyOptional({
    description: '个人介绍'
  })
  @IsOptional()
  @IsString()
  introduction?: string;

  @ApiPropertyOptional({
    description: '来源',
    enum: ['github', 'local', 'google'],
    default: 'local'
  })
  @IsOptional()
  @IsEnum(['github', 'local', 'google'])
  from?: 'github' | 'local' | 'google';

  @ApiProperty({
    description: '角色ID数组',
    type: [Number],
    example: [1, 2]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  roleIds: number[];
}

// 更新用户 DTO
export class UpdateUserDto extends PartialType(CreateUserDto) {
  @ApiPropertyOptional({
    description: '密码',
    minLength: 6
  })
  @IsOptional()
  @IsString()
  @MinLength(6)
  password?: string;
}

// 查询用户 DTO
export class QueryUsersDto {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10
  })
  @IsOptional()
  @Type(() => Number)
  @IsNumber()
  @Min(1)
  @Max(100)
  limit?: number = 10;

  @ApiPropertyOptional({
    description: '搜索关键词（用户名、昵称、邮箱）',
    example: 'john'
  })
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional({
    description: '角色',
    example: 'admin'
  })
  @IsOptional()
  @IsString()
  role?: string;

  @ApiPropertyOptional({
    description: '状态',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  status?: number;

  @ApiPropertyOptional({
    description: '性别',
    enum: [0, 1]
  })
  @IsOptional()
  @Type(() => Number)
  @IsEnum([0, 1])
  gender?: number;

  @ApiPropertyOptional({
    description: '来源',
    enum: ['github', 'local', 'google']
  })
  @IsOptional()
  @IsEnum(['github', 'local', 'google'])
  from?: 'github' | 'local' | 'google';

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'createdAt',
    enum: ['createdAt', 'updatedAt', 'username', 'email', 'lastLoginAt']
  })
  @IsOptional()
  @IsString()
  sortBy?: string = 'createdAt';

  @ApiPropertyOptional({
    description: '排序方向',
    example: 'DESC',
    enum: ['ASC', 'DESC'],
    default: 'DESC'
  })
  @IsOptional()
  @IsString()
  sortOrder?: 'ASC' | 'DESC' = 'DESC';
}

// 修改密码 DTO
export class ChangePasswordDto {
  @ApiProperty({
    description: '当前密码',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  currentPassword: string;

  @ApiProperty({
    description: '新密码',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  newPassword: string;

  @ApiProperty({
    description: '确认新密码',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  confirmPassword: string;
}

// 重置密码 DTO（管理员使用）
export class ResetPasswordDto {
  @ApiProperty({
    description: '新密码',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  newPassword: string;

  @ApiProperty({
    description: '确认新密码',
    minLength: 6
  })
  @IsString()
  @IsNotEmpty()
  @MinLength(6)
  confirmPassword: string;
}

// 批量操作用户 DTO
export class BatchUserActionDto {
  @ApiProperty({
    description: '用户ID数组',
    type: [Number],
    example: [1, 2, 3]
  })
  @IsArray()
  @IsNumber({}, { each: true })
  userIds: number[];

  @ApiProperty({
    description: '操作类型',
    enum: ['activate', 'deactivate', 'delete', 'assignRole']
  })
  @IsString()
  @IsEnum(['activate', 'deactivate', 'delete', 'assignRole'])
  action: 'activate' | 'deactivate' | 'delete' | 'assignRole';

  @ApiPropertyOptional({
    description: '角色ID（用于分配角色操作）',
    type: [Number]
  })
  @IsOptional()
  @IsArray()
  @IsNumber({}, { each: true })
  roleIds?: number[];
}
